import { createRouter, createWebHistory } from 'vue-router'
import { showToast } from 'vant';
import 'vant/es/toast/style';
const routes = [
  {
    path: '/',
    component: () => import('../views/home/Index.vue')
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('../views/home/Index.vue')
  },
  {
    path: '/order',
    name: 'order',
    component: () => import('../views/order/Index.vue'),
    meta:{
      isAuth:true,
    }
  },
  {
    path: '/cart',
    name: 'cart',
    component: () => import('../views/cart/Index.vue'),
    meta:{
      isAuth:true,
    }
  },
  {
    path: '/mine',
    name: 'mine',
    component: () => import('../views/mine/Index.vue'),
    meta:{
      isAuth:true,
    }
  },
  {
    path: '/stores',
    name: 'stores',
    component: () => import('../views/stores/Index.vue')
  },
  {
    path: '/ordercreate',
    name: 'ordercreate',
    component: () => import('../views/orderOrcreate'),
    meta:{
      isAuth:true,
    }
  },
  {
    path: '/address',
    name: 'address',
    component: () => import('../views/address/Index.vue'),
    meta:{
      isAuth:true,
    }
  },
  {
    path: '/addressedit',
    name: 'addressedit',
    component: () => import('../views/addressEdit/index.vue'),
    meta:{
      isAuth:true,
    }
  },
  {
    path: '/userinfoedit',
    name: 'userinfoedit',
    component: () => import('../views/userinfo/index.vue'),
    meta:{
      isAuth:true,
    }
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login/index.vue'),

  },
  {
    path: '/register',
    name: 'register',
    component: () => import('../views/register/index.vue')
  },
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})
// 全局路由守卫
router.beforeEach((to,from,next)=>{
  if(to.meta.isAuth){
    // 登录成功后会完本地存储，存储token
    if(localStorage.isLogin == 'token'){
      next()
    }else{
      next('/login');
      showToast('请去登录')
    }
  }else{
    next()
  }
})
export default router
